﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jim.K3Cloud;

/// <summary>
/// 业务查询
/// </summary>
[Sdk("Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.QueryBusinessInfo", "业务查询")]
public class K3CloudQueryBusinessInfoRequest : ISdkRequest<K3CloudQueryBusinessInfoResponse>  ,IK3CloudCustomRequest
{
    [Required(ErrorMessage ="表单Id不能为空")]
    public string FormId { get; set; }

    public object QueryParams()
    {
        return new {
            Data = new {
                FormId = FormId
            }
        };
    }

    public void Validate()
    {
    }
}

public class K3CloudQueryBusinessInfoRequestData
{
}

public class K3CloudQueryBusinessInfoResponse : SdkResponse<K3CloudQueryBusinessInfoNeedReturnData>
{
}

public class K3CloudQueryBusinessInfoNeedReturnData
{ 
    public K3CloudQueryBusinessInfoData NeedReturnData { get; set; }
}

public class K3CloudQueryBusinessInfoData
{
    public string Id { get; set; }
    [JsonConverter(typeof(LocaleValueConverter))]
    public string Name { get; set; }
    public List<K3CloudQueryBusinessInfoEntry> Entrys { get; set; }
    public List<K3CloudMetaDataOperation> Operations { get; set; }
    public string MasterPKFieldName { get; set; }
    public string PkFieldName { get; set; }
    [JsonConverter(typeof(TypeConverter))]
    public string PkFieldType { get; set; }
    public K3CloudMetaDataLinkTable LinkSet { get; set; }
}

public class K3CloudQueryBusinessInfoEntry
{
    public string Id { get; set; }
    public string Key { get; set; }

    [JsonConverter(typeof(LocaleValueConverter))]
    public string Name { get; set; }
    public string TableName { get; set; }
    public string ParentKey { get; set; }
    public string EntryName { get; set; }
    public string EntryPkFieldName { get; set; }
    [JsonConverter(typeof(TypeConverter))]
    public string  PkFieldType { get; set; }
    public List<K3CloudQueryBusinessInfoField> Fields { get; set; }
    public string EntityType { get; set; }
    public string SeqFieldKey { get; set; }
}

public class K3CloudQueryBusinessInfoField
{
    public string Key { get; set; }
    [JsonConverter(typeof(LocaleValueConverter))]
    public string Name { get; set; }
    public string FieldName { get; set; }
    public string PropertyName { get; set; }
    public int FieldType { get; set; }
    public string EntityKey { get; set; }
    public string TableName { get; set; }
    public ElementType ElementType { get; set; }
    public int MustInput { get; set; }
    public string LookUpObjectFormId { get; set; }
    //
    // 摘要:
    //     基础资料检索对象编码,(辅助资料类型)
    public string LookUpObjectID { get; set; }
    public string EnumObjectId { get; set; }
    public List<K3CloudMetaDataExtend> Extends { get; set; }
    public string ControlFieldKey { get; set; }
    public string GroupFieldTableName { get; set; }

    public string GetEnum()
    {
        var sb = new StringBuilder();
        this.Extends.ForEach(u => sb.Append($"{u.Value}:{u.Caption};"));
        return sb.ToString();
    }

    public string GetFieldType()
    {
        var fieldType = "";
        switch (this.FieldType)
        {
            case 52:
                fieldType = "bool";
                break;
            case 56:
                fieldType = "int";
                break;
            case 61:
                fieldType = "DateTime";
                break;
            case 106:
                fieldType = "decimal";
                break;
            case 167:
                fieldType = "string";
                break;
            case 231:
                fieldType = "string";
                break;
            case 175:
                if (this.ElementType == ElementType.CHECKBOXFIELD)
                    fieldType = "bool";
                else
                    fieldType = "string";
                break;
            case 127:
                fieldType = string.IsNullOrEmpty(this.LookUpObjectFormId) ? "" : $"K3CloudSimple{this.LookUpObjectFormId}";
                break;
        }
        return fieldType;
    }
}

public class K3CloudMetaDataExtend
{
    public string Value { get; set; }
    public string Caption { get; set; }
    public int Seq { get; set; }
    public bool Invalid { get; set; }
}

public class K3CloudMetaDataOperation
{
    public long OperationId { get; set; }
    public string OperationNumber { get; set; }
    [JsonConverter(typeof(LocaleValueConverter))]
    public string OperationName { get; set; }

}

public class K3CloudMetaDataLinkTable
{

    public bool IsNeedLinkTable { get; set; }
    public List<K3CloudMetaDataLinkEntity> LinkEntitys { get; set; }
}

public class K3CloudMetaDataLinkEntity
{
    public string ParentEntityKey { get; set; }
    //
    // 摘要:
    //     对应单据类型字段
    public string SrcBillTypeKey { get; set; }
    //
    // 摘要:
    //     单据编号字段
    public string SrcBillNoKey { get; set; }
    //
    // 摘要:
    //     Key值
    public string Key { get; set; }
    //
    // 摘要:
    //     关联表名
    public string TableName { get; set; }
    //
    // 摘要:
    //     发生关联时的关键字段列表；如基本单位数量，需记录合并前的源单携带量，以便按实际携带量反写
    //
    // 言论：
    //     支持1到n个
    public List<string> WriteBackFieldKeys { get; set; }
}